export class hd {
	
	/*
	* return : if pc return false, if mobile return true
	*/
	pc_or_mobile = () => /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)

	baidutongji = () => {
		let hm = document.createElement("script");
		let s = document.getElementsByTagName("script")[0];
		if(this.pc_or_mobile){
			hm.src = "https://hm.baidu.com/hm.js?a34193dc92085b7d3e1e746627ce435b";
			s.parentNode.insertBefore(hm, s);
		}else {
			hm.src = "https://hm.baidu.com/hm.js?9cf3075d2f6b03afedf5cf5de9e612bd";
			s.parentNode.insertBefore(hm, s);
		}
	}

	getCookie = (name, key)=> {
		let reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        let arr = document.cookie.match(reg);
        if (arr != null) return unescape(arr[2]).substring(key);
        else return "";
	}

	getUrlParam = (name) => {
		let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        let r = window.location.search.trim().substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        else return ""; //返回参数值
	}

	setUrlParam = () =>{
		let uidParam = this.getCookie('uidCookie') != "" ? this.getCookie('uidCookie', 4) : this.getUrlParam('uid');  //"uid=" 长度为"4"
		let cpParam = this.getCookie('cpCookie') != "" ? this.getCookie('cpCookie', 4) : this.getUrlParam('coupon');  //"cid=" 长度为"4"
		let wxParam = this.getCookie('wxCookie') != "" ? this.getCookie('wxCookie', 4) : this.getUrlParam('wxnumber');  //"wid=" 长度为"4"
		//let addUid = uidParam!=""?"uid=" + uidParam:"";
		let addUid = "uid=" + uidParam;
		let addCid = "coupon=" + cpParam;
		let addWid = "wxnumber=" + wxParam;
		document.querySelectorAll("a").forEach(function (i) {
			let $href = i.href;
			if ($href.indexOf("uid=") >= 0)
				return;
			let haveParam = $href.indexOf("?") >= 0 ? "1" : "0";//判断是否已经有参数
			if (haveParam == "1") {
				if (uidParam != "" && cpParam != "" && wxParam == "") { i.href = $href + "&" + addUid + "&" + addCid; }
				if (uidParam != "" && cpParam != "" && wxParam != "") { i.href = $href + "&" + addUid + "&" + addCid + "&" + addWid; }
				if (uidParam != "" && cpParam == "" && wxParam != "") { i.href = $href + "&" + addUid + "&" + addWid; }
				if (uidParam != "" && cpParam == "" && wxParam == "") { i.href = $href + "&" + addUid; }
				if (uidParam == "" && cpParam != "" && wxParam != "") { i.href = $href + "&" + addCid + "&" + addWid; }
				if (uidParam == "" && cpParam != "" && wxParam == "") { i.href = $href + "&" + addCid; }
				if (uidParam == "" && cpParam == "" && wxParam != "") { i.href = $href + "&" + addWid; }
				if (uidParam == "" && cpParam == "" && wxParam == "") { i.href = $href; }
			}
			if (haveParam == "0") {
				if (uidParam != "" && cpParam != "" && wxParam != "") { i.href = $href + "?" + addUid + "&" + addCid + "&" + addWid; }
				if (uidParam != "" && cpParam != "" && wxParam == "") { i.href = $href + "?" + addUid + "&" + addCid; }
				if (uidParam != "" && cpParam == "" && wxParam != "") { i.href = $href + "?" + addUid + "&" + addWid; }
				if (uidParam != "" && cpParam == "" && wxParam == "") { i.href = $href + "?" + addUid; }
				if (uidParam == "" && cpParam != "" && wxParam != "") { i.href = $href + "?" + addCid + "&" + addWid; }
				if (uidParam == "" && cpParam != "" && wxParam == "") { i.href = $href + "?" + addCid; }
				if (uidParam == "" && cpParam == "" && wxParam != "") { i.href = $href + "?" + addWid; }
				if (uidParam == "" && cpParam == "" && wxParam == "") { i.href = $href; }
			}
    });
	}


}

